<template>
    <div class="fillcontain">
        <!-- <head-top></head-top> -->
        <div class="table_container" >
            <el-collapse v-model="activeNames" accordion>
            <el-collapse-item title="查询条件" name="1">

                <el-row :gutter="20">
                    <el-col :span="20">
                        <div class="demo-input-suffix" >
                            <el-select v-model="yhqid" filterable clearable placeholder="请选择优惠券">
                                <el-option
                                v-for="item in yhqList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>  
                            <el-select v-model="status" clearable placeholder="请选择状态">
                                <el-option
                                v-for="item in statuslist"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>  
                            <el-select v-model="mallid" clearable placeholder="请选择门店">
                                <el-option
                                v-for="item in mallList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>                                    
                        </div>
                    </el-col>
                    <el-col :span="4" >
                        <div style="text-align:right">
                        <el-button type="primary" icon="el-icon-search" :loading="searchLoading" @click="doSearch()" >查询</el-button>
                        </div>
                    </el-col>
                </el-row>    
            </el-collapse-item>
            </el-collapse>
        </div>
        <div class="table_container" >

            <el-row :gutter="10">
                <el-col :span="20">
                    <div class="demo-input-suffix" >
                               
                    </div>
                </el-col>
                <el-col :span="4" >
                    <div style="text-align:right">
                    <el-button round type="primary" icon="el-icon-plus" size="mini" @click="doAdd()" >添加</el-button>
                    </div>
                </el-col>
            </el-row>    
            
        </div>        
        <div class="table_container">
            <el-table
                :data="tableData"
                style="width: 100%" stripe>
                <el-table-column type="expand">
                  <template scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                      <el-form-item label="单据 ID">
                        <span>{{ props.row.id }}</span>
                      </el-form-item>
                      <el-form-item label="优惠券名称">
                        <span>{{ props.row.yhqname }}</span>
                      </el-form-item>
                      <el-form-item label="门店名称">
                        <span>{{ props.row.mallname }}</span>
                      </el-form-item>
                      <el-form-item label="开始日期">
                        <span>{{ props.row.ksrq }}</span>
                      </el-form-item>
                      <el-form-item label="结束日期">
                        <span>{{ props.row.jsrq }}</span>
                      </el-form-item>
                      <el-form-item label="编辑人">
                        <span>{{ props.row.djrmc }}</span>
                      </el-form-item> 
                      <el-form-item label="编辑日期">
                        <span>{{ props.row.djsj }}</span>
                      </el-form-item>
                      <el-form-item label="启动人">
                        <span>{{ props.row.qdrmc }}</span>
                      </el-form-item> 
                      <el-form-item label="启动日期">
                        <span>{{ props.row.qdsj }}</span>
                      </el-form-item>  
                      <el-form-item label="终止人">
                        <span>{{ props.row.zzrmc }}</span>
                      </el-form-item> 
                      <el-form-item label="终止日期">
                        <span>{{ props.row.zzsj }}</span>
                      </el-form-item>   
                      <el-form-item label="库存数量">
                        <span>{{ props.row.kcsl }}</span>
                      </el-form-item> 
                      <el-form-item label="单日库存限量">
                        <el-checkbox v-model="props.row.bDRKCXL" disabled > </el-checkbox>
                      </el-form-item>  
                      <el-form-item label="个人限量">
                        <span>{{ props.row.grkcsl }}</span>
                      </el-form-item> 
                      <el-form-item label="单日个人限量">
                        <el-checkbox v-model="props.row.bDRGRXL" disabled > </el-checkbox>
                      </el-form-item>   
                      <el-form-item label="不参加活动">
                        <el-checkbox v-model="props.row.bBcjhdbj" disabled > </el-checkbox>
                      </el-form-item>                                                                                                                
                      <el-form-item label="状态">
                        <span>{{ props.row.statusname }}</span>
                      </el-form-item>                                                               
                    </el-form>
                  </template>
                </el-table-column>
                <el-table-column label="单据 ID"  prop="id"></el-table-column>
                <el-table-column  label="优惠券名称" prop="yhqname"></el-table-column>
                <el-table-column label="门店名称" prop="mallname"></el-table-column>
                <el-table-column label="库存数量" prop="kcsl"></el-table-column>
                <el-table-column  label="单日库存限量">
                    <template slot-scope="scope">
                        <el-checkbox v-model="scope.row.bDRKCXL" disabled > </el-checkbox>
                    </template>
                </el-table-column>
                <el-table-column label="个人库存数量" prop="grkcsl"></el-table-column>
                <el-table-column label="单日个人限量">
                     <template slot-scope="scope">
                        <el-checkbox v-model="scope.row.bDRGRXL" disabled > </el-checkbox>
                    </template>                   
                </el-table-column>   
                <el-table-column label="不参加活动">
                     <template slot-scope="scope">
                        <el-checkbox v-model="scope.row.bBcjhdbj" disabled > </el-checkbox>
                    </template>                   
                </el-table-column>   
 
                <el-table-column label="状态" prop="statusname"></el-table-column>
                <el-table-column label="操作" width="200">
                 <!--  <template scope="scope">
                    <el-button
                      size="mini"
                      @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        
                    <el-button
                      size="mini"
                      type="danger"
                      @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                  </template> -->
                  <template slot-scope="scope">
                        <el-dropdown >
                        <span class="el-dropdown-link">
                             <i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="editgz" @click.native="handleEditGzdx(scope.$index, scope.row)"><i class="el-icon-setting"></i> 配置规则</el-dropdown-item>
                            <el-dropdown-item command="edit" @click.native="handleEdit(scope.$index, scope.row)"><i class="el-icon-edit"></i> 编辑</el-dropdown-item>
                            <el-dropdown-item command="start" divided :disabled="scope.row.status==1"  @click.native="handleDisabled(scope.$index, scope.row,1)">启用</el-dropdown-item>
                            <el-dropdown-item command="end" :disabled="scope.row.status==2"  @click.native="handleDisabled(scope.$index, scope.row,2)">禁用</el-dropdown-item>
                            <el-dropdown-item command="del" divided :disabled="scope.row.status>0"  @click.native="handleDelete(scope.$index, scope.row)">删除</el-dropdown-item>
                        </el-dropdown-menu>
                        </el-dropdown>
                   </template>
                </el-table-column>
            </el-table>
            <div class="Pagination">
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="currentPage"
                  :page-size="20"
                  layout="total, prev, pager, next"
                  :total="count">
                </el-pagination>
            </div>
            <el-dialog title="修改优惠券发券单据" v-model="dialogFormVisible" :visible.sync="dialogFormVisible">
                <el-form :model="selectTable" :rules="rules" ref="editForm">
                    <el-form-item label="优惠券" label-width="100px" prop="yhqid">
                        <el-select v-model="selectTable.yhqid">
                            <el-option
                            v-for="item in yhqSelList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>                    
                    </el-form-item>   
                    <el-form-item label="门店" label-width="100px" prop="mdid">
                        <el-select v-model="selectTable.mdid">
                            <el-option
                            v-for="item in mallList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>                    
                    </el-form-item>    
                    <el-form-item label=" " label-width="100px">
                         <el-checkbox v-model="selectTable.bBcjhdbj" >不参加活动标记</el-checkbox>
                    </el-form-item>  
                    <el-form-item label="开始日期" label-width="100px" prop="ksrq">
                        <el-date-picker v-model="selectTable.ksrq" type="date" placeholder="选择开始日期"
                          format="yyyy-MM-dd"  value-format="yyyy-MM-dd">
                        </el-date-picker>    
                    </el-form-item>
                     <el-form-item label="结束日期" label-width="100px" prop="jsrq">
                        <el-date-picker  v-model="selectTable.jsrq" type="date" placeholder="选择结束日期"
                            @change="jsrqChange" value-format="yyyy-MM-dd">
                        </el-date-picker>    
                    </el-form-item>
                    <el-form-item label="总库存限量" label-width="100px">
                        <el-input-number v-model="selectTable.kcsl" auto-complete="off"></el-input-number>
                    </el-form-item>   
                    <el-form-item label=" " label-width="100px">
                        <el-checkbox v-model="selectTable.bDRKCXL" >总库存单日限量标记</el-checkbox>
                    </el-form-item>  
                    <el-form-item label="个人限量" label-width="100px">
                        <el-input-number v-model="selectTable.grkcsl" auto-complete="off"></el-input-number>
                    </el-form-item>   
                    <el-form-item label=" " label-width="100px">
                        <el-checkbox v-model="selectTable.bDRGRXL" >个人单日限量标记</el-checkbox>
                    </el-form-item>   
 
                    <el-form-item label="状态" label-width="100px">
                        <el-select v-model="selectTable.status" disabled>
                            <el-option
                            v-for="item in statuslist"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>                    
                    </el-form-item>               
                </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="doSaveYhqffd">确 定</el-button>
              </div>
            </el-dialog>
            <el-dialog title="配置优惠券发券规则" v-model="gzdxDialogVisible" :visible.sync="gzdxDialogVisible" width="80%">
                <template>
                    <div class="fillcontain">
                        <!-- <head-top></head-top> -->
                        <div class="table_container" >
                            <el-row  >
                                <el-col :span="24" >
                                    <div style="text-align:right">
                                        <el-button-group>
                                            <el-button type="primary" size="mini" :disabled="curStatus!=bBrowse" ref="btnAddChild" icon="el-icon-plus" @click="doAddGzdx()">添加</el-button>
                                            <el-button type="primary" size="mini" :disabled="curStatus!=bBrowse" icon="el-icon-edit" @click="doEditGzdx()">修改</el-button>
                                            <el-button type="primary" size="mini" :disabled="curStatus==bBrowse" icon="el-icon-check" @click="doSaveGzdx()">保存</el-button> 
                                            <el-button type="primary" size="mini" :disabled="curStatus==bBrowse" icon="el-icon-close" @click="doGzdxCancel()">取消</el-button>
                                            <el-button type="primary" size="mini" icon="el-icon-delete" :disabled="curStatus!=bBrowse" @click="doDeleteGzdx()">删除</el-button>
                                            <el-button type="primary" size="mini" icon="el-icon-circle-close" :disabled="curStatus!=bBrowse" @click="doCloseGzdx()">关闭</el-button>
                                        </el-button-group>
                                    </div>
                                </el-col>
                            </el-row>    
                            <el-row  >
                                <el-col :span="8" >
                                    <div class="table_container">
                                        <el-table :data="tableDataGzdx" ref="gzdxtable" style="width: 100%" stripe highlight-current-row
                                                    @current-change="gzdxCurrentChange">
                                            <el-table-column label="对象编号"  prop="dxid"></el-table-column>
                                            <el-table-column  label="规则名称" prop="gzmc"></el-table-column> 
                                            <el-table-column  label="优先级" prop="yxj"></el-table-column> 
                                        </el-table>     
                                    </div>
                                </el-col>
                                <el-col :span="16" >
                                        <el-form :model="selectGzdx" :rules="gzdxrules" ref="editGzdxForm">
                                            <!-- <el-form-item> -->
                                             <!--    <template> -->
                                                    <el-tabs v-model="activeName" @tab-click="handleClick">
                                                        <el-tab-pane label="基本信息" name="first">
                                                            <el-form-item label="规则" label-width="100px" prop="gzid">
                                                                <el-select v-model="selectGzdx.gzid" :disabled="curStatus==bBrowse">
                                                                    <el-option
                                                                    v-for="item in ruleLists"
                                                                    :key="item.value"
                                                                    :label="item.label"
                                                                    :value="item.value" :disabled="item.disabled">
                                                                    </el-option>
                                                                </el-select>                    
                                                            </el-form-item>    
                                                            <el-form-item label="优先级" label-width="100px" width="100%">
                                                                <el-input-number v-model="selectGzdx.yxj" auto-complete="off" :disabled="curStatus==bBrowse"></el-input-number>
                                                            </el-form-item>   
 
                                                        </el-tab-pane>
                                                        <el-tab-pane label="店铺信息" name="second">
                                                            <el-tree
                                                                :data="shopLists" 
                                                                show-checkbox
                                                                node-key="id"
                                                                ref="shoptree"
                                                                highlight-current
                                                                :props="defaultProps">
                                                            </el-tree>  
                                                        </el-tab-pane>                            
                                                        <el-tab-pane label="商品信息" name="third">
                                                            <el-cascader
                                                                clearable placeholder="请选择店铺"
                                                                :options="shopList"
                                                                v-model="curShopOptions"
                                                                @change="handleChange">
                                                            </el-cascader>  
                                                            <template>
                                                                <el-transfer v-model="spids" :data="goodsLists" :titles="['待选商品', '已选商品']">
                                                                </el-transfer>
                                                            </template>

                                                        </el-tab-pane>
                                                    </el-tabs>
                                               <!--  </template>    -->                                
                                           <!--  </el-form-item>       -->                                                
                        
                                        </el-form>
                                     
                                </el-col>
                            </el-row>                   
                        </div>
                        
                    </div>
                </template>
            </el-dialog>            
        </div>
    </div>
</template>

<script>
    import headTop from '../components/headTop'
    import {baseUrl, baseImgPath} from '@/config/env'
    import { getYhqffdLists,getCoupDefAllLists,saveYhqffd,getGrantRuleForSel,getYhqffdDpLists,
    getshopListsForSel,getGoodsLists,saveYhqffdGzdx,delYhqffdGzdx,
    getYhqffdGzdxLists,deleteYhqffd,disableYhqffd,getshopListsForTree,getMallLists} from '@/api/getData'
    export default {
        data(){
            return {
                baseUrl,
                baseImgPath,
                bAdd:1,
                bEdit:2,
                bBrowse:0,
                curStatus:null,
                searchLoading:false,
                activeNames: ['1'],
                bEditGzdx:false,
                mallid:'',
                mallList:[],
                yhqid:'',
                yhqList:[],
                yhqSelList:[],
                status:'',
                shopLists:[],
                shopList:[],
                goodsLists:[],
                ruleLists:[],
                deptids:[],
                spids:[],
                spLists:[],
                statuslist:[{
                        value: '0',
                        label: '未启动'
                    }, {
                        value: '1',
                        label: '已启动'
                    }, {
                        value: '2',
                        label: '手工终止'
                    }, {
                        value: '3',
                        label: '自动终止'
                    }
                ],
                rules : {
                    ksrq : [
                        { required: true, message: '请选择开始日期', trigger: 'blur' }
                    ],
                    jsrq : [
                        { required: true, message: '请选择结束日期', trigger: 'blur' }
                    ],
                    yhqid : [
                        { required: true, message: '请选择开始日期', trigger: 'blur' }
                    ],
                    mdid : [
                        { required: true, message: '请选择结束日期', trigger: 'blur' }
                    ]
                },  
                gzdxrules : {  
                },   
                activeName: 'first', 
                curShopOptions:[], //根据当前的店铺id来生成数组                             
                curId:null,
                selectIndex: null,
                city: {},
                offset: 0,
                limit: 20,
                count: 0,
                tableData: [],
                tableDataItem: [],
                currentPage: 1,
                tableDataGzdx: [],
                selectTable: {},
                selectGzdx: {},
                dialogFormVisible: false,
                gzdxDialogVisible: false,
                categoryOptions: [],
                selectedCategory: [],
                address: {},
                yhqname:'',
                defaultProps: {
                    children: 'children',
                    label: 'label'
                }
            }
        },
        created(){
            this.initData();
        },
    	components: {
    		headTop,
    	},
        methods: {
            async initData(){
                try{
                /*     this.city = await cityGuess();
                    const countData = await getResturantsCount();
                    if (countData.status == 1) {
                        this.count = countData.count;
                    }else{
                        throw new Error('获取数据失败');
                    } */
                    this.doGetCoupDefLists();
                    this.getMallLists();
                    this.doGetShopTreeLists();
                    this.doGetShopLists();
                    this.doGetYhqffdLists();
                    this.doGetGrantRuleForSel();
                }catch(err){
                    console.log('获取数据失败', err);
                }
            },
            async getMallLists(){
                var _this = this
                var data = {
                    page:1,
                    rows:999,
                    yttype:0
                }
                const malls = await getMallLists(data);
                this.mallList = [];
                _this.count = malls.total
                malls.rows.forEach(item => {
                    const tableData = {};
                    tableData.value = item.id.toString();
                    tableData.label = item.name;
                    this.mallList.push(tableData);
                })
                console.log('mallList:',this.mallList)
            }, 
            async getGoodsLists(){
                var _this = this
                var data = {
                    page:1,
                    rows:999,
                    shopid:_this.curShopOptions[_this.curShopOptions.length-1]
                }
                const spLists = await getGoodsLists(data);
                //this.tableData = [];
                spLists.rows.forEach(item => {
                    const tableData = {};
                    tableData.key = item.sp_id
                    tableData.label = item.name;
                    if(!this.IsContain(this.goodsLists,item.sp_id)){
                        this.goodsLists.push(tableData);
                    } 
                })
            }, 
            IsContain(arr,value)
            {
                for(var i=0;i<arr.length;i++)
                {
                    if(arr[i].key==value)
                    return true;
                }
                return false;
            },                       
            async doGetShopLists(){
                var _this = this
                var data = {
                    page:1,
                    rows:999
                }
                const shops = await getshopListsForSel(data);
                this.shopList = [];
                shops.forEach(item => {
                    const tableData = {};
                    tableData.value = item.value;
                    tableData.label = item.label;
                    tableData.children = item.children;
                    this.shopList.push(tableData);
                })
                console.log('shopList:',this.shopList)
            },   
            async doGetGrantRuleForSel(){
                var _this = this
                var data = {
                }
                const rules = await getGrantRuleForSel(data);
                this.ruleLists = [];
                rules.rows.forEach(item => {
                    const tableData = {};
                    tableData.value = item.id;
                    tableData.label = item.rulename;
                    if(item.status>1){
                        tableData.disabled = true
                    }
                    this.ruleLists.push(tableData);
                })
                console.log('ruleLists:',this.ruleLists)
            },  
            async doGetYhqffdDpLists(val){
                var _this = this
                var data = {
                   id:val.id,
                   dxid:val.dxid
                }
                const deptlist = await getYhqffdDpLists(data);
                this.deptids = [];
               // _this.treeLists = skfslist.rows 
                deptlist.DP.forEach(item => {                 
                    this.deptids.push(item.shopid);
                }) 
                this.goodsLists = [];
               // _this.treeLists = skfslist.rows 
                this.goodsLists = [];
                this.spids = []
                deptlist.SP.forEach(item => {   
                    const tableData = {};
                    tableData.key = item.spid;
                    tableData.label = item.spname;                   
                    this.goodsLists.push(tableData);
                    this.spids.push(item.spid);
                })                 
                console.log('deptids:',this.deptids)  
            },                       
            async doGetShopTreeLists(){
                var _this = this
                var data = {
                    page:1,
                    rows:999,
                    yttype:0
                }
                const malllist = await getshopListsForTree(data);
                console.log('shopLists:',malllist) 
                this.shopLists = [];
               // _this.treeLists = skfslist.rows 
                    malllist.forEach(item => {
                    const tableData = {};
                    tableData.id = item.id;
                    tableData.label = item.label;
                    tableData.children = item.children;
                    
                    this.shopLists.push(tableData);
                }) 
                console.log('shopLists:',this.shopLists)  
            },              
            async doGetCoupDefLists(){
                var _this = this
                var data = {
                }
                const coupDefs = await getCoupDefAllLists(data);
                this.yhqList = [];
                coupDefs.rows.forEach(item => {
                    const tableData = {};
                    tableData.value = item.yhqid.toString();
                    tableData.label = item.yhqmc;
                    this.yhqList.push(tableData);
                })
                console.log('yhqList:',this.yhqList)
            },
            async doGetCoupDefListsForSel(val){
                var _this = this
                var data = {
                    status:val
                }
                const coupDefs = await getCoupDefAllLists(data);
                this.yhqSelList = [];
                coupDefs.rows.forEach(item => {
                    const tableData = {};
                    tableData.value = item.yhqid.toString();
                    tableData.label = item.yhqmc;
                    this.yhqSelList.push(tableData);
                })
                console.log('yhqSelList:',this.yhqSelList)
            },                            
            async doGetYhqffdLists(){
                var _this = this
                var data = {
                    page:_this.currentPage,
                    rows:_this.limit,
                    yhqid:_this.yhqid,
                    status:_this.status,
                    mdid:_this.mallid
                }
                const rules = await getYhqffdLists(data);
                this.tableData = [];
                _this.count = rules.total
                rules.rows.forEach(item => {
                    const tableData = {};
                    tableData.id = item.id;
                    tableData.yhqid = item.yhqid.toString();
                    tableData.yhqname = item.yhqname;
                    tableData.mdid = item.mdid.toString();
                    tableData.mallname = item.mallname;
                    tableData.ksrq = item.ksrq.split(" ",1)[0];
                    tableData.jsrq = item.jsrq.split(" ",1)[0];
                    tableData.kcsl = item.kcsl;
                    tableData.kcsl_flag = item.kcsl_flag;
                    tableData.bDRKCXL = (item.kcsl_flag==1)?true:false;
                    tableData.grkcsl = item.grkcsl;
                    tableData.grkcsl_flag = item.grkcsl_flag;
                    tableData.bDRGRXL = (item.grkcsl_flag==1)?true:false;                   
                    tableData.djr = item.djr;
                    tableData.djrmc = item.djrmc;
                    tableData.djsj = item.djsj;
                    tableData.bcjhdbj = item.bcjhdbj;
                    tableData.bBcjhdbj = (item.bcjhdbj==1)?true:false;
                    tableData.status = item.status;
                    tableData.statusname = item.statusname;
                    this.tableData.push(tableData);
                })
            },
            async doGetYhqffdGzdxLists(val){
                var _this = this
                var data = {
                    id:val
                }
                const items = await getYhqffdGzdxLists(data);
                this.tableDataGzdx = [];
                items.data.forEach(item => {
                    const tableData = {};
                    tableData.id = item.id;
                    tableData.dxid = item.dxid;
                    tableData.gzid = item.gzid;
                    tableData.yxj = item.yxj;
                    tableData.gzmc= item.gzmc
                    this.tableDataGzdx.push(tableData);
                })
            },            
            getyttypeName(val){
                if(val==0){
                    return '百货'
                }else if(val==1){
                    return '超市'
                }else if(val==2){
                    return '异业联盟'
                }else{
                    return ''
                }
            },
            handleChange(value) {
                console.log(value);
                this.getGoodsLists();
            },               
            ksrqChange(val) {
                console.log(val)
                this.selectTable.ksrq=val
            }, 
            jsrqChange(val) {
                console.log(val)
                this.selectTable.jsrq=val
            },  
            handleClick(tab, event) {
                console.log(tab, event);
            },                                    
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.offset = (val - 1)*this.limit;
                this.doGetYhqffdLists()
            },
            gzdxCurrentChange(val) {
                var _this = this
                this.selectGzdx.id = val.id
                this.selectGzdx.dxid = val.dxid
                this.selectGzdx.yxj = val.yxj
                this.selectGzdx.gzid = val.gzid
                this.doGetYhqffdDpLists(val).then(res=>{
                    setTimeout(() => {
                        _this.$refs['shoptree'].setCheckedKeys(this.deptids);
                    }, 1);
                })                 
               // this.doGetYhqffdLists()
            }, 
            doAdd() {
                this.selectTable ={status:'0'}
                this.dialogFormVisible = true;
                this.doGetCoupDefListsForSel(0);//只选择状态正常的优惠券
            },                        
            handleEdit(index, row) {
                console.log('handleEdit:',row)
                this.curId = row.id;
                this.selectTable = row;
                this.selectTable.status =  row.status.toString();
              //  this.address.address = row.address;
                this.dialogFormVisible = true;
                //this.doGetCoupPayRuleItem(row.id)
                this.doGetCoupDefListsForSel(null);
                
            },
            handleEditGzdx(index, row) {
                console.log('handleEdit:',row)
                this.curId = row.id;
                this.selectTable = row;
                this.selectTable.status =  row.status.toString();
                this.curStatus=this.bBrowse;
              //  this.address.address = row.address;
                this.gzdxDialogVisible = true;
                this.doGetYhqffdGzdxLists(row.id).then(()=>{
                    this.$refs.gzdxtable.setCurrentRow(this.tableDataGzdx[0]);
                })
                //this.doGetCoupPayRuleItem(row.id)
               // this.doGetCoupDefListsForSel(null);
            },   
                    
            doAddGzdx() {
                this.curStatus=this.bAdd
                this.activeName='first'
                this.selectGzdx = {id:this.selectTable.id,gzid:'',dxid:null}
                this.spids=[]
                this.goodsLists=[]
                console.log('selectGzdx:',this.selectGzdx)
               // this.selectTable ={status:'0',bLJ:true,bLadder:true}
               // this.dialogFormVisible = true;
               // this.doGetCoupDefListsForSel(0);//只选择状态正常的优惠券
            },
            doEditGzdx() {
                this.curStatus=this.bEdit
               // this.selectTable ={status:'0',bLJ:true,bLadder:true}
               // this.dialogFormVisible = true;
               // this.doGetCoupDefListsForSel(0);//只选择状态正常的优惠券
            }, 
            doCloseGzdx() {
                this.gzdxDialogVisible = false;
            },              
            async doSaveGzdx() {
                try{ 
                    this.selectGzdx.shopids = this.$refs.shoptree.getCheckedKeys()
                    this.selectGzdx.spids = this.spids
                    const res = await saveYhqffdGzdx(this.selectGzdx)
                    if (res.Status == 0) {
                        this.$message({
                            type: 'success',
                            message: '保存规则对象信息成功'
                        });
                       // this.doGetXtczyLists();
                  /*       if(this.curStatus==this.bAdd){
                           this.tableDataGzdx.push(this.selectGzdx)
                        } */
                        
                        this.doGetYhqffdGzdxLists(this.selectGzdx.id).then(()=>{
                            const dxid = this.tableDataGzdx.length-1
                            this.$refs.gzdxtable.setCurrentRow(this.tableDataGzdx[dxid]);
                        })                        
                        this.curStatus=this.bBrowse
                    }else{
                        this.$message({
                            type: 'error',
                            message: res.msg
                        });
                    }
                }catch(err){
                    console.log('保存规则对象信息失败', err);
                }

            },  
            async doDeleteGzdx(){      
                this.$confirm('此操作将删除该发券规则对象, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async() => {
                        const res = await delYhqffdGzdx(this.selectGzdx)
                        if (res.Status == 0) {
                            this.$message({
                                type: 'success',
                                message: '删除发券单据规则对象成功'
                            });
                            //this.tableData.splice(index, 1);
                            this.doGetYhqffdGzdxLists(this.selectGzdx.id).then(()=>{
                                const dxid = this.tableDataGzdx.length-1
                                this.$refs.gzdxtable.setCurrentRow(this.tableDataGzdx[dxid]);
                            })                        
                            this.curStatus=this.bBrowse                            
                        }else{
                            this.$message({
                                type: 'error',
                                message: res.msg
                            }); 
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '取消删除'
                    });          
                });
            },            
            


            doGzdxCancel() {
                this.curStatus=this.bBrowse
               // this.selectTable ={status:'0',bLJ:true,bLadder:true}
               // this.dialogFormVisible = true;
               // this.doGetCoupDefListsForSel(0);//只选择状态正常的优惠券
            },                       
            addFood(index, row){
                this.$router.push({ path: 'addGoods', query: { restaurant_id: row.id }})
            },
            async handleDelete(index, row) {
                var _this = this
               _this.doDeleteYhqffd(row.id,index)
            },
            async handleDisabled(index, row,status) {
                var _this = this
               _this.doDisableYhqffd(index,row.id,status)
            },            
            async querySearchAsync(queryString, cb) {
                if (queryString) {
                    try{
                        const cityList = await searchplace(this.city.id, queryString);
                        if (cityList instanceof Array) {
                            cityList.map(item => {
                                item.value = item.address;
                                return item;
                            })
                            cb(cityList)
                        }
                    }catch(err){
                        console.log(err)
                    }
                }
            },
            AddRule(){
                var data={id:this.curId,salemoney:null,paymoney:null,idindex:null}
                console.log('data:',data)
                this.tableDataItem.push(data);
                console.log('tableDataItem:',this.tableDataItem)
            },
            
            doSearch(){
                this.currentPage=1;
                this.searchLoading=true;
                this.doGetYhqffdLists();
                this.searchLoading=false;
            },    
            handleCommand(command,row) {
                //this.$message('click on item ' + command);
                console.log('row:',row)
                this.selectTable = row;
              //  this.address.address = row.address;
                this.dialogFormVisible = true;
            },        
            handleServiceAvatarScucess(res, file) {
                if (res.status == 1) {
                    this.selectTable.image_path = res.image_path;
                }else{
                    this.$message.error('上传图片失败！');
                }
            },
            async doSaveYhqffd(){ 
                try{
                    this.$refs['editForm'].validate( async (valid) => {
                        if (valid) {
                            this.selectTable.bcjhdbj = (this.selectTable.bBcjhdbj==true)?1:0;
                            this.selectTable.kcsl_flag = (this.selectTable.bDRKCXL==true)?1:0;
                            this.selectTable.grkcsl_flag =(this.selectTable.bDRGRXL==true)?1:0;
                            this.selectTable.djsj=null;
                         //   this.selectTable.items = this.tableDataItem
                            const res = await saveYhqffd(this.selectTable)
                            if (res.Status == 0) {
                                this.dialogFormVisible = false;
                                this.$message({
                                    type: 'success',
                                    message: '保存优惠券用券规则成功'
                                });
                                this.doGetYhqffdLists();
                            }else{
                                this.$message({
                                    type: 'error',
                                    message: res.msg
                                });
                            }
                        }else{
                            console.log('error submit!!');
                            return;
                        }  
                    });      
                }catch(err){
                    console.log('保存优惠券用券规则失败', err);
                }
            },
            doDeleteYhqffd(id,index) {
                var idList = []
                idList[0] = id           
                this.$confirm('此操作将删除该用券单据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async() => {
                        const res = await deleteYhqffd(idList);
                        if (res.Status == 0) {
                            this.$message({
                                type: 'success',
                                message: '删除用券单据成功'
                            });
                            this.tableData.splice(index, 1);
                        }else{
                            this.$message({
                                type: 'error',
                                message: res.msg
                            }); 
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '取消删除'
                    });          
                });
            },  
            doDisableYhqffd(index,id,istatus) {
                var opermsg = ''
                if(istatus==1){
                    opermsg='启用'
                }else{
                    opermsg='禁用'
                }
                var data={id:id,status:istatus}
                this.$confirm('此操作将'+opermsg+'该用券规则, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async() => {
                        const res = await disableYhqffd(data);
                        if (res.Status == 0) {
                            this.$message({
                                type: 'success',
                                message: opermsg+'用券规则成功'
                            });
                            this.doGetYhqffdLists();
                           // this.tableData.splice(index, 1);
                        }else{
                            this.$message({
                                type: 'error',
                                message: res.msg
                            }); 
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '取消'+opermsg
                    });          
                });
            },              
        },
    }
</script>

<style lang="less">
	@import '../style/mixin';
    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
    .table_container{
        padding: 10px 20px;
    }
    .Pagination{
        display: flex;
        justify-content: flex-start;
        margin-top: 8px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #20a0ff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
    .avatar {
        width: 120px;
        height: 120px;
        display: block;
    }
    .demo-input-suffix {
        margin-bottom: 15px;
    }
    .demo-input.demo-zh-CN .el-input {
        width: 180px;
    } 
    .demo-input-suffix .el-input {
        margin-right: 15px;
        width:auto
    }


</style>
